package com.jack.helloworld.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Random;

public class StringUtils {

	public static final int RANDOM_ALL = 1;
	public static final int RANDOM_CHARACTER = 2;
	public static final int RANDOM_DIGIT = 3;
	private static Random random = new Random();
	private static final StringBuffer D_BUFFER = new StringBuffer("0123456789");
	private static final StringBuffer C_BUFFER = new StringBuffer("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");

	private static final StringBuffer ALL_BUFFER = new StringBuffer("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");

	public static boolean isBlank(String str) {
		int strLen;
		if ((str == null) || ((strLen = str.length()) == 0))
			return true;
		for (int i = 0; i < strLen; i++) {
			if (!Character.isWhitespace(str.charAt(i))) {
				return false;
			}
		}
		return true;
	}

	public static boolean isNotBlank(String str) {
		return !isBlank(str);
	}

	public static final String byteArrayToHexString(byte[] data) {
		if ((data == null) || (data.length == 0)) {
			throw new IllegalArgumentException();
		}
		StringBuilder sb = new StringBuilder(data.length * 2);
		byte[] arrayOfByte = data;
		int j = data.length;
		for (int i = 0; i < j; i++) {
			byte b = arrayOfByte[i];
			int v = b & 0xFF;
			if (v < 16) {
				sb.append('0');
			}
			sb.append(Integer.toHexString(v));
		}
		return sb.toString().toUpperCase();
	}

	public static byte[] hexStringToByteArray(String s) {
		if (isBlank(s)) {
			throw new IllegalArgumentException();
		}
		s = s.trim();
		int len = s.length();
		byte[] d = new byte[len / 2];
		for (int i = 0; i < len; i += 2) {
			d[(i / 2)] = ((byte) ((Character.digit(s.charAt(i), 16) << 4) + Character
					.digit(s.charAt(i + 1), 16)));
		}
		return d;
	}

	public static String[] parseUrlForCmwap(String url) {
		if (isBlank(url)) {
			throw new IllegalArgumentException();
		}
		String[] ss = { "", "" };

		url = url.trim();
		int index = url.indexOf("http://");
		if (index >= 0) {
			url = url.substring(index + 7);
		}
		index = url.indexOf("/");
		if (index > 0) {
			ss[0] = url.substring(0, index);
			ss[1] = url.substring(index);
		} else {
			ss[0] = url;
			ss[1] = "/";
		}
		System.out.println("BaseLib"
				+ "StringUtils.parseUrlForCmwap() => domain:" + ss[0]
				+ " | path:" + ss[1]);

		return ss;
	}

	public static String getFileNameFromUrl(String url) {
		if (isBlank(url)) {
			throw new IllegalArgumentException();
		}
		String filename = null;
		url = url.trim();
		int index = url.lastIndexOf('?');
		if (index > 1) {
			url = url.substring(0, index);
		}
		index = url.lastIndexOf("/");
		if (index >= 0)
			filename = url.substring(index + 1);
		else {
			filename = url;
		}
		return filename;
	}

	public static String removeUrlParams(String url) {
		if (isBlank(url)) {
			throw new IllegalArgumentException();
		}

		int index = url.indexOf("?");
		if (index >= 0) {
			return url.substring(0, index);
		}
		return url;
	}

	public static boolean isEmpty(String str) {
		return (str == null) || (str.length() == 0);
	}

	public static boolean isNotEmpty(String str) {
		return !isEmpty(str);
	}

	public static String getRandomString(int length, int type) {
		StringBuffer sb = new StringBuffer();
		int range = 0;
		if (type == 1)
			range = ALL_BUFFER.length();
		else if (type == 2)
			range = C_BUFFER.length();
		else if (type == 3) {
			range = D_BUFFER.length();
		}

		for (int i = 0; i < length; i++) {
			if (type == 1)
				sb.append(ALL_BUFFER.charAt(random.nextInt(range)));
			else if (type == 2)
				sb.append(C_BUFFER.charAt(random.nextInt(range)));
			else if (type == 3) {
				sb.append(D_BUFFER.charAt(random.nextInt(range)));
			}
		}

		return sb.toString();
	}

	public static String encodeUTF8(String str) {
		if (str == null)
			return "";
		if ("".equals(str))
			return "";
		try {
			return URLEncoder.encode(str, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			System.err.println("BaseLib" + e.getMessage());
		}
		return "";
	}

	public static String getFileSuffix(String fileName) {
		if (isBlank(fileName)) {
			return "";
		}

		int index = fileName.lastIndexOf(".");
		if (index >= 0) {
			return fileName.substring(index + 1);
		}
		return "";
	}

}
